package com.semlab.server.services;

import com.semlab.server.enrichment.DataMiner;
import com.semlab.server.graph.ConceptRepository;
import com.semlab.server.graph.ProfileRepository;
import com.semlab.server.resources.Profile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.facebook.api.Facebook;
import org.springframework.social.facebook.api.FacebookProfile;
import org.springframework.social.facebook.api.impl.FacebookTemplate;
import org.springframework.stereotype.Component;

import com.semlab.server.domain.Client;


@Component
public class ClientService {

    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    ProfileRepository profiles;

    public Client login(String id, String token) {
        try {
            log.debug("logging in {} {}", id, token);
            Facebook facebook = new FacebookTemplate(token);
            FacebookProfile profile = facebook.userOperations().getUserProfile();
            Client client = new Client(id, token);
            client.setFirstName(profile.getFirstName());
            client.setLastName(profile.getLastName());
            Profile p = profiles.getById(id);
            if (p != null)
                client.setImported(profiles.getById(id).isImported());
//        client.setImported(miner.isImported(id));
            return client;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("error={}", e.getMessage());
        }
        return null;
    }

}
